package com.loveyukari.controller;


import com.loveyukari.model.LiwuStyle;
import com.loveyukari.service.LiwuService;
import com.loveyukari.service.RoomService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@Controller
@RequestMapping("/room/liwu")
public class RoomLiwuController {


    @Autowired
    private RoomService roomService;
    @Autowired
    private LiwuService liwuService;

    @RequestMapping("/getTotalCount/{gfid}")
    @ResponseBody
    public JSONObject getTotal(@PathVariable int gfid){
        LiwuStyle liwuStyle = liwuService.getLiwuStyleByGfid(gfid);
        int total = roomService.getTotal(gfid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("liwu",liwuStyle.getName());
        jsonObject.put("count",total);
        return jsonObject;
    }

    @RequestMapping(value="/getDayCount",produces="text/plain;charset=UTF-8")
    @ResponseBody
    public String getDayCount(int gfid,long time) throws UnsupportedEncodingException {
        long ct = System.currentTimeMillis() - time;
        int total = 0;
        if (ct > 86400000){
            if (ct < 86460000){
                return "wait";
            } else {
                total = roomService.getTotal(gfid,new Date(time));
            }
        } else if (ct >= 0){
            total = roomService.getTotal(gfid);
        } else {
            return "统计未开始";
        }
        LiwuStyle liwuStyle = liwuService.getLiwuStyleByGfid(gfid);
        String result = total+"";
        return result;
    }




}
